package org.example.projectdemo.controller;

/**
 * ClassName: ScheduleController
 * Package: org.example.projectdemo.controller
 * Description:
 *
 * @Author ZY
 * @Create 00:02
 * @Version 1.0
 */

import jakarta.validation.Valid;
import org.example.projectdemo.model.entity.Schedule;
import org.example.projectdemo.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

/**
 * 排课管理控制器
 * 处理课程安排相关的HTTP请求
 */
@RestController
@RequestMapping("/api/schedules")
public class ScheduleController {
    @Autowired
    private ScheduleService scheduleService;

    /**
     * 创建排课信息
     *
     * @param schedule 排课信息
     * @return 创建后的排课信息
     */
    @PostMapping
    public ResponseEntity<Schedule> createSchedule(@Valid @RequestBody Schedule schedule) {
        Schedule savedSchedule = scheduleService.createSchedule(schedule);
        return ResponseEntity.ok(savedSchedule);
    }

    /**
     * 更新排课信息
     *
     * @param id 排课ID
     * @param schedule 排课信息
     * @return 更新后的排课信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<Schedule> updateSchedule(@PathVariable Long id, 
                                                   @Valid @RequestBody Schedule schedule) {
        schedule.setId(id);
        try {
            Schedule updatedSchedule = scheduleService.updateSchedule(schedule);
            return ResponseEntity.ok(updatedSchedule);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().build();
        }
    }

    /**
     * 根据ID获取排课信息
     *
     * @param id 排课ID
     * @return 排课信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Schedule> getScheduleById(@PathVariable Long id) {
        Optional<Schedule> schedule = scheduleService.getScheduleById(id);
        return schedule.map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    /**
     * 获取所有排课列表
     *
     * @return 所有排课列表
     */
    @GetMapping
    public ResponseEntity<List<Schedule>> getAllSchedules() {
        List<Schedule> schedules = scheduleService.getAllSchedules();
        return ResponseEntity.ok(schedules);
    }

    /**
     * 根据课程ID获取排课列表
     *
     * @param courseId 课程ID
     * @return 该课程的排课列表
     */
    @GetMapping("/by-course/{courseId}")
    public ResponseEntity<List<Schedule>> getSchedulesByCourse(@PathVariable Long courseId) {
        List<Schedule> schedules = scheduleService.getSchedulesByCourseId(courseId);
        return ResponseEntity.ok(schedules);
    }

    /**
     * 根据日期获取排课列表
     *
     * @param date 日期 (格式: yyyy-MM-dd)
     * @return 指定日期的排课列表
     */
    @GetMapping("/by-date/{date}")
    public ResponseEntity<List<Schedule>> getSchedulesByDate(@PathVariable LocalDate date) {
        List<Schedule> schedules = scheduleService.getSchedulesByDate(date);
        return ResponseEntity.ok(schedules);
    }

    /**
     * 根据日期范围获取排课列表
     *
     * @param startDate 开始日期 (格式: yyyy-MM-dd)
     * @param endDate 结束日期 (格式: yyyy-MM-dd)
     * @return 指定日期范围的排课列表
     */
    @GetMapping("/by-date-range")
    public ResponseEntity<List<Schedule>> getSchedulesByDateRange(
            @RequestParam LocalDate startDate,
            @RequestParam LocalDate endDate) {
        List<Schedule> schedules = scheduleService.getSchedulesByDateBetween(startDate, endDate);
        return ResponseEntity.ok(schedules);
    }

    /**
     * 删除排课信息
     *
     * @param id 排课ID
     * @return 删除成功返回200
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteSchedule(@PathVariable Long id) {
        try {
            System.out.println("尝试删除排课，ID: " + id);
            if (!scheduleService.existsScheduleById(id)) {
                System.out.println("排课不存在，ID: " + id);
                return ResponseEntity.notFound().build();
            }
            System.out.println("排课存在，开始删除，ID: " + id);
            scheduleService.deleteSchedule(id);
            System.out.println("排课删除成功，ID: " + id);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            System.err.println("删除排课异常，ID: " + id + ", 错误: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.status(500).body("删除排课失败: " + e.getMessage());
        }
    }
}